import selenium
from selenium import webdriver
import pathlib
import time
from selenium.webdriver.common.keys import Keys

# 基本信息
# 视频存放路径
catalog_mp4 = r"C:\Users\Administrator\Desktop\视频发布"
# 视频描述
describe = "裸眼3D看《白蛇2：青蛇崛起》今日惊蛰，蛇虫横行，你我姐妹两条蛇，掀翻这金山寺 #青蛇 #裸眼3D #AI #国漫 #视觉震撼"
time.sleep(10)
options = webdriver.ChromeOptions()
options.add_experimental_option("debuggerAddress", "127.0.0.1:5003")
driver = webdriver.Chrome(options=options)

path = pathlib.Path(catalog_mp4)

# 视频地址获取
path_mp4 = ""
for i in path.iterdir():
    if (".mp4" in str(i)):
        path_mp4 = str(i);
        break;

if (path_mp4 != ""):
    print("检查到视频路径：" + path_mp4)
else:
    print("未检查到视频路径，程序终止！")
    exit()

# 封面地址获取
path_cover = ""
for i in path.iterdir():
    if (".png" in str(i) or ".jpg" in str(i)):
        path_cover = str(i);
        break;

if (path_cover != ""):
    print("检查到封面路径：" + path_cover)
else:
    print("未检查到封面路径，程序终止！")
    exit()


def publish_yidian():
    '''
     作用：发布一点视频
    '''

    # 进入一点号创作者页面，并上传视频
    driver.get("https://mp.yidianzixun.com/#/Writing/videoEditor")
    time.sleep(2)
    driver.find_element(By.XPATH,'//input[@type="file"]').send_keys(path_mp4)

    # 等待视频上传完成
    while True:
        time.sleep(3)
        try:
            driver.find_element(By.XPATH,'//*[text()="上传成功"]')
            break;
        except Exception as e:
            print("视频还在上传中···")

    print("视频已上传完成！")

    # 输入标题，30个文字限制处理
    time.sleep(3)
    driver.find_element(By.XPATH,'//*[@class="title row"]//input').clear()
    time.sleep(4)
    title = describe[:describe.index(" #")]
    if (len(title) > 30):
        title = title[:27] + "..."
    driver.find_element(By.XPATH,'//*[@class="title row"]//input').send_keys(title)

    # 选择分类
    time.sleep(1)
    driver.find_element(By.XPATH,'//*[@class="cascader-value"]//input').click()
    time.sleep(2)
    driver.find_element(By.XPATH,'//*[contains(text(),"电影")]').click()
    time.sleep(1)
    driver.find_element(By.XPATH,'//*[contains(text(),"设置默认分类")]').click()

    # 添加封面
    time.sleep(1)
    driver.find_element(By.XPATH,'//*[text()="设置视频封面"]').click()
    # time.sleep(1)
    # driver.find_element(By.XPATH,'//*[text()="视频截图"]').click()
    # time.sleep(1)
    # driver.find_element(By.XPATH,'//*[text()="确定"]').click()
    # time.sleep(3)
    driver.find_element(By.XPATH,'//*[contains(text(),"本地上传")]').click()
    time.sleep(1)
    driver.find_element(By.XPATH,'//*[text()="上传封面"]//input[@type="file"]').send_keys(path_cover)
    time.sleep(3)
    driver.find_element(By.XPATH,'//*[text()="确定"]').click()
    time.sleep(3)

    # 添加标签
    time.sleep(1)
    driver.find_element(By.XPATH,'//*[@class="tagsuginput-container"]//input').send_keys("裸眼3D")
    driver.find_element(By.XPATH,'//*[@class="tagsuginput-container"]//input').send_keys(Keys.ENTER)
    time.sleep(1)
    driver.find_element(By.XPATH,'//*[@class="tagsuginput-container"]//input').send_keys("视觉震撼")
    driver.find_element(By.XPATH,'//*[@class="tagsuginput-container"]//input').send_keys(Keys.ENTER)
    time.sleep(1)
    driver.find_element(By.XPATH,'//*[@class="tagsuginput-container"]//input').send_keys("影视剪辑")
    driver.find_element(By.XPATH,'//*[@class="tagsuginput-container"]//input').send_keys(Keys.ENTER)
    time.sleep(1)
    driver.find_element(By.XPATH,'//*[@class="tagsuginput-container"]//input').send_keys("电影赏析")
    driver.find_element(By.XPATH,'//*[@class="tagsuginput-container"]//input').send_keys(Keys.ENTER)
    time.sleep(1)
    driver.find_element(By.XPATH,'//*[@class="tagsuginput-container"]//input').send_keys("深度学习")
    driver.find_element(By.XPATH,'//*[@class="tagsuginput-container"]//input').send_keys(Keys.ENTER)
    time.sleep(1)
    driver.find_element(By.XPATH,'//*[@class="tagsuginput-container"]//input').send_keys("AI")
    driver.find_element(By.XPATH,'//*[@class="tagsuginput-container"]//input').send_keys(Keys.ENTER)
    time.sleep(1)

    # 输入视频描述
    driver.find_element(By.XPATH,'//textarea').send_keys(describe)

    # 人工进行检查并发布
    # time.sleep(3)
    # # 点击发布
    # driver.find_element(By.XPATH,'//*[text()="发布"]').click()
    # time.sleep(1)
    # driver.find_element(By.XPATH,'//*[text()="确定"]').click()


# 开始执行视频发布
publish_yidian()